package cn.cloud.butler.scheduled.schedule;

import cn.cloud.butler.scheduled.DateUtils;
import cn.cloud.butler.scheduled.domain.OperStateEntity;
import cn.cloud.butler.scheduled.domain.StateDayEntity;
import cn.cloud.butler.scheduled.repository.OperStateRepository;
import cn.cloud.butler.scheduled.repository.StateDayRepository;
import com.sun.jmx.snmp.Timestamp;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * All rights Reserved,Designed By TangYue
 *
 * @author: 汤越
 * @date: 2019/10/11 14:04
 * @copyright: @2019
 */

@Component
//@RestController
public class OnlineJudgeSchedule {

    private static final Logger logger = LogManager.getLogger(cn.cloud.butler.scheduled.schedule.OnlineJudgeSchedule.class);

    @Autowired
    StateDayRepository stateDayRepository;
    @Autowired
    OperStateRepository operStateRepository;

    
//    @Scheduled(cron = "0 0/7 * * * ? ")
    //@GetMapping("/e")
    protected void executeSchedule() {
        //logger.info("online jude once ...");
        //获取所有状态为4000的
        List<String> stateList = new ArrayList<>();
        stateList.add("1000");
        stateList.add("2000");
        stateList.add("3000");

        handleOfflineDevice(stateDayRepository.findByStateIn(stateList));
    }


    //获取离线列表
    private void handleOfflineDevice(List<StateDayEntity> stateDayList) {
        logger.info("state day number is :" + stateDayList.size());
        //计算时间差
        for (StateDayEntity stateDayEntity : stateDayList) {
            Long iTimeSpan = DateUtils.DataDiff(stateDayEntity.getUploadTime().toString(), DateUtils.getDateTimeFormat(new Date()));
             //超过时间， 判定离线
            if (iTimeSpan > 6) {
                Integer stateTime  = Math.toIntExact((System.currentTimeMillis() -stateDayEntity.getUploadTime().getTime()) /1000);
                saveOffLineDevice(stateTime, stateDayEntity);
            }
        }
    }

    //写入离线状态
    @Transactional
    public void saveOffLineDevice(Integer stateTime, StateDayEntity stateDayEntity){
        try{
            Date currentTime = new Date();
            //写入stateDay
            stateDayEntity.setState("4000");
            stateDayEntity.setUploadTime(currentTime);
            stateDayEntity.setCheckTime(currentTime);
            stateDayRepository.save(stateDayEntity);

            //写入operState表
            operStateRepository.save(new OperStateEntity(stateDayEntity.getMacSn(), "4000",
                    currentTime, "timer", currentTime,stateTime));

            logger.warn("insert offline success: MacSN为 " + stateDayEntity.getMacSn() +" 当前时间为" + currentTime);


        }catch (Exception e){
            logger.warn("insert offline device failed: " + e.toString());
        }

    }
}
